// pages/video/video.js
import request from '../../utils/requests'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        videoGroupList:[],
        navId:'',
        videoList:[],
        videoId: '',
        videoUpdateTime:[],
        isTriggered:false,
    },

    handleEnded(event){
        let contextvideo = wx.createVideoContext(event.currentTarget.id);
        let videoupdatetime = this.data.videoUpdateTime;
        let videoitem = videoupdatetime.find(item => item.vid === event.currentTarget.id);
        if(videoitem){
            videoitem.currentTime = 0;
        }
        this.setData({
            videoUpdateTime:videoupdatetime
        })
        contextvideo.stop();
    },

    toSearch(){
        wx.navigateTo({
          url: '/pages/search/search',
        })
    },

    handleTimeUpdate(event){
        let videoobj = {vid:event.currentTarget.id,currentTime:event.detail.currentTime};
        let videoUpdateTime = this.data.videoUpdateTime;
        let videoitem = videoUpdateTime.find(item => item.vid === videoobj.vid);
        if(videoitem){
            videoitem.currentTime = event.detail.currentTime;
        }else{
            videoUpdateTime.push(videoobj);
        }
        this.setData({
            videoUpdateTime
        })
    },

    async getvideoGroupListdata(){
        let result = await request('/video/group/list');
        if(result.code === 200){
            this.setData({
                videoGroupList:result.data.slice(0,20),
                navId: result.data[0].id
            })
            this.getvideoDataList(this.data.navId); 
        }
    },

    async getvideoDataList(navId){
        let result = await request('/video/group',{id:navId});
        if(result.code === 200){
            let index = 0;
            let videoList = result.datas.map(item => {
                item.id = index++;
                return item;
            })
            wx.hideLoading({
              success: (res) => {},
            })
            this.setData({
                videoList,
                isTriggered:false
            })
        }
    },

    changeNav(event){
        let navId = event.currentTarget.id;
        this.setData({
            navId:navId >>> 0, //navId类型转换成number
            videoList: []
        })
        wx.showLoading({
          title: '正在加载',
        })
        this.getvideoDataList(this.data.navId);
    },
    handlePlay(event){
        // let vid = event.currentTarget.id;
        // this.vid !==vid && this.videoContext  && this.videoContext.stop();
        // this.vid = vid;
        // this.setData({
        //     videoId:vid
        // })
        // this.videoContext = wx.createVideoContext('vid');
        // this.videoContext.play();
        let vid = event.currentTarget.id;
        this.setData({
            videoId: vid
          })
          // 创建控制video标签的实例对象
          this.videoContext = wx.createVideoContext(vid);
          // 判断当前的视频之前是否播放过，是否有播放记录, 如果有，跳转至指定的播放位置
          let {videoUpdateTime} = this.data;
          let videoItem = videoUpdateTime.find(item => item.vid === vid);
          if(videoItem){
            this.videoContext.seek(videoItem.currentTime);
          }
          this.videoContext.play();
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getvideoGroupListdata();

    },

    handleToLower(){
        //console.log("low");
        let videoList = this.data.videoList;
        let tmp = videoList;
        tmp.push(...videoList);
        this.setData({
            videoList:tmp
        })
    },

    handleRefresher(event){
        this.setData({
            isTriggered:true
        })
        this.getvideoDataList(this.data.navId);
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        
    }
})